<?php
/**
 * Created by PhpStorm.
 * User: fusha_000
 * Date: 15-1-7
 * Time: 上午9:51
 * 二级域名
 */

class SubDomain {
    static function IsSubDomain()
    {
        //todo::微信端域名当作一级域名
        if ($_SERVER['HTTP_HOST']=='wx.12301.cc') return false;
        $cnt_dot = substr_count($_SERVER['HTTP_HOST'],'.');
        $isSubDomain = false;
        if ($cnt_dot==2) {
            if ( strpos($_SERVER['HTTP_HOST'], 'www') ===false
                && strpos($_SERVER['HTTP_HOST'], '12301.cc')!==false ) {
                $isSubDomain = true;
            }
        }
        elseif ($cnt_dot==3) {
            $isSubDomain = true;
        }
        return $isSubDomain;
    }

    static function SubDomainConf()
    {
        $host           = str_replace('www.', '', $_SERVER['HTTP_HOST']);
        $applyAccount   = substr($host, 0, strpos($host, '.12301'));
        if (isset($redis) && $redis instanceof Redis) {

        } else {
            $redis = new Redis();
            $redis->connect('10.169.9.198', 6379);
            $redis->auth('myPft!12301!&');
        }
        $redis_key      = "shop_{$applyAccount}";
//        $redis->delete($redis_key);
        $shop           = $redis->get($redis_key);
        if (!$shop) {
            $shop = self::GetShopInfo($redis, $redis_key, $applyAccount);
            if ($shop!==false) return $shop;
            return 404;
        }
        $redis->close();
        return unserialize($shop);
    }

    static function GetShopInfo(Redis $redis, $redis_key, $applyAccount)
    {
        if (!class_exists('go_sql')) {
            include_once('/var/www/html/new/conf/le.je');
            $le = new go_sql();
            $le->connect();
        } else {
            $le = $GLOBALS['le'];
        }
        //step2:根据账号查询该供应商二级域名的配置
        $sql = <<<SQL
SELECT `fid`,`M_domain`, `M_name`, `M_logo1`, `M_logo2`, `M_banner`,
`M_banner_url`, `M_navi`, `M_qq`, `M_tel`, `M_host`,`M_addr`,M_account_domain
FROM pft_member_domain_info WHERE
M_domain='{$applyAccount}' OR M_account_domain='{$applyAccount}' LIMIT 1
SQL;
        $le->query($sql);
        $shop = $le->fetch_assoc();
        if ($shop) {
            $redis->setex($redis_key, 1800, serialize($shop));
            $redis->close();
            return $shop;
        }
        return false;
    }
} 